package com.atguigui.leetcode;

import java.util.Map;
import java.util.TreeMap;

/**
 * 731.我的日程安排表 II
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/7/19 8:39
 */
public class P731MyCalendarIi {

    public static void main(String[] args) {
        MyCalendarTwo solution = new P731MyCalendarIi().new MyCalendarTwo();
        // TO TEST
    }

    class MyCalendarTwo {
        private TreeMap<Integer, Integer> cnt;

        public MyCalendarTwo() {
            cnt = new TreeMap();
        }

        public boolean book(int start, int end) {
//            int ans = 0;
            int maxBook = 0;
            //有重复的就+1
            cnt.put(start, cnt.getOrDefault(start, 0) + 1);
            cnt.put(end, cnt.getOrDefault(end, 0) - 1);
            //遍历所有日历
            for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) {
                int freq = entry.getValue();
                maxBook += freq;
//                ans = Math.max(maxBook, ans);
                if (maxBook > 2) {
                    cnt.put(start, cnt.getOrDefault(start, 0) - 1);
                    cnt.put(end, cnt.getOrDefault(end, 0) + 1);
                    return false;
                }
            }
            return true;
        }
    }
}
